//
//  CustomCollectionViewLayout.m
//  15-HomeWork
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CustomCollectionViewLayout.h"
@interface CustomCollectionViewLayout ()
@property (nonatomic, strong)NSMutableArray<UICollectionViewLayoutAttributes *> *attributesArray;
//@property (nonatomic, assign) CGFloat itemWidth;
//@property (nonatomic, assign) CGFloat contentWidth;
@property (nonatomic, assign) CGFloat  totalWidth;
@property (nonatomic, assign) CGFloat totalHeight;
@end

@implementation CustomCollectionViewLayout
- (instancetype)init {
    self = [super init];
    if (self) {
        _attributesArray = [NSMutableArray array];
        _cellWidth = 100;
        _cellHeight = 100;
        _cellMargin = 0;
        
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        _attributesArray = [NSMutableArray array];
        _cellWidth = 100;
        _cellHeight = 100;
        _cellMargin = 0;
        
    }
    return self;
   
}

- (void)prepareLayout {
    
    
    if (_attributesArray.count != 0) {
        [_attributesArray removeAllObjects];
    }
     _totalWidth = 0.0f;
     _totalHeight = 0.0f;
    for ( int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
        UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexpath];
        [_attributesArray addObject:attribute];
        CGFloat x = _totalWidth;
        CGFloat y = _totalHeight;
        CGFloat width = _cellWidth;
        CGFloat height = _cellHeight;
        attribute.frame = CGRectMake(x, y, width, height);
        _totalWidth += _cellWidth ;
        if (_totalWidth == self.collectionView.bounds.size.width) {
        _totalWidth = 0.0f;
        _totalHeight += _cellHeight;
        }
        
    }
    
    
    
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _attributesArray;
}

- (CGSize)collectionViewContentSize {
    
   return  self.collectionView.bounds.size;
}
@end
